using NUnit.Framework;
using System;
using System.Collections;
using System.Diagnostics;

using BuildAutoIncrement;

namespace TestUnits {

    [TestFixture]
	public class CommandLineArgsTest {
		public CommandLineArgsTest() {
            Projects1 = SplitArgs(ProjectsArg1);
            Projects2 = SplitArgs(ProjectsArg2);
        }

        [SetUp] 
        public void SetUp() {
        }

        [TearDown] 
        public void TearDown() {
        }


        public void TestGuiSwitch() {
            string[] args = new string[] {"/g", SolutionFilename};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.None);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.NotDefined);
            Assert.IsTrue(cla.NewVersion.Length == 0);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 0);
            Assert.IsTrue(cla.ProjectsToForce.Length == 0);
            Assert.IsTrue(cla.ProjectsToExclude.Length == 0);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsTrue(cla.StartGui);

            args = new string[] {SolutionFilename, "/g"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsTrue(cla.StartGui);

            args = new string[] {SolutionFilename, "/G"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsTrue(cla.StartGui);
        }


        public void TestApplyToSwitch() {
            string[] args = new string[] {SolutionFilename, "/a:a"};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.None);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.NotDefined);
            Assert.IsTrue(cla.NewVersion.Length == 0);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 0);
            Assert.IsTrue(cla.ProjectsToForce.Length == 0);
            Assert.IsTrue(cla.ProjectsToExclude.Length == 0);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsFalse(cla.StartGui);

            args = new string[] {SolutionFilename, "/A:A"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/a:A"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/A:a"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/a:m"};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/A:M"};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/a:M"};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/A:m"};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
        }


        public void TestIncludeProjectsSwitch() {
            string[] args = new string[] {SolutionFilename, "/p:"+ProjectsArg1};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.None);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.NotDefined);
            Assert.IsTrue(cla.NewVersion.Length == 0);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects1.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, Projects1));
            Assert.IsTrue(cla.ProjectsToForce.Length == 0);
            Assert.IsTrue(cla.ProjectsToExclude.Length == 0);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsFalse(cla.StartGui);

            args = new string[] {SolutionFilename, "/P:"+ProjectsArg1};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects1.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, Projects1));

            args = new string[] {SolutionFilename, "/p:"+ProjectsArg2};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, Projects2));

            args = new string[] {SolutionFilename, "/P:"+ProjectsArg2};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, Projects2));

            args = new string[] {SolutionFilename, "/p:"+ProjectsArg1+","+ProjectsArg2};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects1.Length + Projects2.Length);
            ArrayList combined = new ArrayList(Projects1);
            combined.AddRange(Projects2);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, (string[])combined.ToArray(typeof(string))));

            args = new string[] {SolutionFilename, "/P:"+ProjectsArg1+","+ProjectsArg2};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects1.Length + Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, (string[])combined.ToArray(typeof(string))));

            args = new string[] {SolutionFilename, "/p:"+ProjectsArg2+","+ProjectsArg1};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects1.Length + Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, (string[])combined.ToArray(typeof(string))));

            args = new string[] {SolutionFilename, "/P:"+ProjectsArg2+","+ProjectsArg1};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects1.Length + Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, (string[])combined.ToArray(typeof(string))));

            args = new string[] {SolutionFilename, "/P:+project1,+project2,+project3,+project4"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 4);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, new string[] {"project1", "project2", "project3", "project4"}));

            args = new string[] {SolutionFilename, "/P:+project1,project2,project3,+project4"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 4);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, new string[] {"project1", "project2", "project3", "project4"}));

            args = new string[] {SolutionFilename, "/P:+\"project1\",+\"project2\",+\"project3\",+\"project4\""};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 4);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, new string[] {"project1", "project2", "project3", "project4"}));

            args = new string[] {SolutionFilename, "/P:+\"project1\",\"project2\",\"project3\",+\"project4\""};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 4);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, new string[] {"project1", "project2", "project3", "project4"}));
        }

        public void TestExcludeProjectsSwitch() {
            string[] args = new string[] {SolutionFilename, "/p:-project1,-project2,-project3"};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.None);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.NotDefined);
            Assert.IsTrue(cla.NewVersion.Length == 0);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 0);
            Assert.IsTrue(cla.ProjectsToForce.Length == 0);
            Assert.IsTrue(cla.ProjectsToExclude.Length == Projects1.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToExclude, Projects1));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsFalse(cla.StartGui);

            args = new string[] {SolutionFilename, "/P:-project1,-project2,-project3"};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsToExclude.Length == Projects1.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToExclude, Projects1));

            args = new string[] {SolutionFilename, "/p:-\"project 4\",-\"project 5\",-\"project 6\""};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsToExclude.Length == Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToExclude, Projects2));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsFalse(cla.StartGui);

            args = new string[] {SolutionFilename, "/P:-\"project 4\",-\"project 5\",-\"project 6\""};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsToExclude.Length == Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToExclude, Projects2));

            args = new string[] {SolutionFilename, "/P:+project1,+project2,+project3,-\"project 4\",-\"project 5\",-\"project 6\""};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == Projects1.Length);
            Assert.IsTrue(cla.ProjectsToExclude.Length == Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, Projects1));
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToExclude, Projects2));
        }

        public void TestForceProjectsSwitch() {
            string[] args = new string[] {SolutionFilename, "/p:!project1,!project2,!project3"};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.None);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.NotDefined);
            Assert.IsTrue(cla.NewVersion.Length == 0);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 0);
            Assert.IsTrue(cla.ProjectsToExclude.Length == 0);
            Assert.IsTrue(cla.ProjectsToForce.Length == Projects1.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToForce, Projects1));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsFalse(cla.StartGui);

            args = new string[] {SolutionFilename, "/P:!project1,!project2,!project3"};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsToForce.Length == Projects1.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToForce, Projects1));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/p:!\"project 4\",!\"project 5\",!\"project 6\""};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsToForce.Length == Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToForce, Projects2));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/P:!\"project 4\",!\"project 5\",!\"project 6\""};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsToForce.Length == Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToForce, Projects2));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/P:-\"project1\",-\"project2\",-\"project3\",!\"project 4\",!\"project 5\",!\"project 6\""};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsToExclude.Length == Projects1.Length);
            Assert.IsTrue(cla.ProjectsToForce.Length == Projects2.Length);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToForce, Projects2));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/P:project1,-project2,!project3,!\"project 4\",-\"project 5\",+\"project 6\""};
            cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 2);
            Assert.IsTrue(cla.ProjectsToExclude.Length == 2);
            Assert.IsTrue(cla.ProjectsToForce.Length == 2);
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsAllowedForVersionModification, new string[] { "project1", "project 6" }));
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToExclude, new string[] { "project2", "project 5" }));
            Assert.IsTrue(ArraysContainSameStrings(cla.ProjectsToForce, new string[] { "project3", "project 4" }));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            try {
                args = new string[] {SolutionFilename, "/P:project1,-project1"};
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException exception) {
                Console.WriteLine(exception.Message);
            }
        }

        public void TestVersionTypesSwitch() {
            string[] args = new string[] {SolutionFilename, "/v:*"};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.ApplyToAllProjects);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.All);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.NotDefined);
            Assert.IsTrue(cla.NewVersion.Length == 0);
            Assert.IsTrue(cla.ProjectsAllowedForVersionModification.Length == 0);
            Assert.IsTrue(cla.ProjectsToExclude.Length == 0);
            Assert.IsTrue(cla.ProjectsToForce.Length == 0);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));
            Assert.IsFalse(cla.StartGui);

            args = new string[] {SolutionFilename, "/V:*"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.All);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:a"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:A"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.AssemblyVersion);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:P"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.AssemblyInformationalVersion);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:p"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.AssemblyInformationalVersion);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:I"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.AssemblyInformationalVersion);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:i"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.AssemblyInformationalVersion);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:F"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.AssemblyFileVersion);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:f"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.AssemblyFileVersion);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:AFI"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.All);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:afi"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == AssemblyVersionType.All);
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:AF"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == (AssemblyVersionType.AssemblyVersion | AssemblyVersionType.AssemblyFileVersion));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:af"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == (AssemblyVersionType.AssemblyVersion | AssemblyVersionType.AssemblyFileVersion));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:FP"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == (AssemblyVersionType.AssemblyFileVersion | AssemblyVersionType.AssemblyInformationalVersion));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            args = new string[] {SolutionFilename, "/v:ai"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.ApplyToTypes == (AssemblyVersionType.AssemblyVersion | AssemblyVersionType.AssemblyInformationalVersion));
            Assert.IsTrue(cla.SolutionFilename.EndsWith(SolutionFilename));

            try {
                args = new string[] {SolutionFilename, "/v:a*"};
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException exception) {
                Console.WriteLine(exception.Message);
            }

            try {
                args = new string[] {SolutionFilename, "/v:aA"};
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException exception) {
                Console.WriteLine(exception.Message);
            }

            try {
                args = new string[] {SolutionFilename, "/v:ip"};
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException exception) {
                Console.WriteLine(exception.Message);
            }
        }


        public void TestDisplaySummarySwitch() {
            string[] args = new string[] {SolutionFilename};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            args = new string[] {SolutionFilename, "/m:n"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            args = new string[] {SolutionFilename, "/M:N"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            args = new string[] {SolutionFilename, "/m:c"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            args = new string[] {SolutionFilename, "/M:C"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            args = new string[] {SolutionFilename, "/m:f"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            args = new string[] {SolutionFilename, "/m:fc"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            args = new string[] {SolutionFilename, "/m:cf"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            args = new string[] {SolutionFilename, "/m:CF"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == string.Empty);

            string filename = "output.txt";
            string quotedfilename = string.Format("\"{0}\"", filename);
            args = new string[] {SolutionFilename, "/m:f" + quotedfilename};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == filename);

            args = new string[] {SolutionFilename, "/m:F" + quotedfilename};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == false);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == filename);

            args = new string[] {SolutionFilename, "/m:f" + quotedfilename + "c"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToConsole == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.ToFile == true);
            Assert.IsTrue(cla.DisplaySummaryOptions.SummaryFilename == filename);

            args = new string[] {SolutionFilename, "/m"};
            try {
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            args = new string[] {SolutionFilename, "/m:"};
            try {
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            args = new string[] {SolutionFilename, "/m:fcd"};
            try {
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            args = new string[] {SolutionFilename, "/m:fcn"};
            try {
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            args = new string[] {SolutionFilename, "/m:nfcd"};
            try {
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            args = new string[] {SolutionFilename, "/m:f\"hell"};
            try {
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            args = new string[] {SolutionFilename, "/m:f\"hell\"c\""};
            try {
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }
        }

        public void TestSynchronizeSwitch() {
            string[] args = new string[] {SolutionFilename};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.NotDefined);

            args = new string[] {SolutionFilename, "/s:s"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.Synchronize);

            args = new string[] {SolutionFilename, "/S:S"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.Synchronize);

            args = new string[] {SolutionFilename, "/s:i"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.IncrementAndSynchronize);

            args = new string[] {SolutionFilename, "/S:I"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.IncrementAndSynchronize);

            args = new string[] {SolutionFilename, "/s:n"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.None);

            args = new string[] {SolutionFilename, "/S:N"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SynchronizeProjects == CommandLineArgs.ProjectsSynchronization.None);

            try {
                args = new string[] {SolutionFilename, "/s"};
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            try {
                args = new string[] {SolutionFilename, "/S"};
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            try {
                args = new string[] {SolutionFilename, "/s:x"};
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }

            try {
                args = new string[] {SolutionFilename, "/S:X"};
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }
        }

        public void TestSourceSafeUserSwitch() {
            string[] args = new string[] {SolutionFilename};
            CommandLineArgs cla = new CommandLineArgs(args);
            Assert.IsFalse(cla.SourceSafeUserOptions.PromptForUsername);
            Assert.IsFalse(cla.SourceSafeUserOptions.PromptForPassword);

            args = new string[] {SolutionFilename, "/u"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SourceSafeUserOptions.PromptForUsername);
            Assert.IsTrue(cla.SourceSafeUserOptions.PromptForPassword);

            args = new string[] {SolutionFilename, "/U"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SourceSafeUserOptions.PromptForUsername);
            Assert.IsTrue(cla.SourceSafeUserOptions.PromptForPassword);

            args = new string[] {SolutionFilename, "/u:user"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SourceSafeUserOptions.Username == "user");
            Assert.IsFalse(cla.SourceSafeUserOptions.PromptForUsername);
            Assert.IsTrue(cla.SourceSafeUserOptions.PromptForPassword);

            args = new string[] {SolutionFilename, "/u:user,"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SourceSafeUserOptions.Username == "user");
            Assert.IsFalse(cla.SourceSafeUserOptions.PromptForUsername);
            Assert.IsTrue(cla.SourceSafeUserOptions.Password == "");
            Assert.IsFalse(cla.SourceSafeUserOptions.PromptForPassword);

            args = new string[] {SolutionFilename, "/u:user,password"};
            cla = new CommandLineArgs(args);
            Assert.IsTrue(cla.SourceSafeUserOptions.Username == "user");
            Assert.IsFalse(cla.SourceSafeUserOptions.PromptForUsername);
            Assert.IsTrue(cla.SourceSafeUserOptions.Password == "password");
            Assert.IsFalse(cla.SourceSafeUserOptions.PromptForPassword);

            args = new string[] {SolutionFilename, "/u:"};
            try {
                cla = new CommandLineArgs(args);
                Assert.Fail("Should throw exception");
            }
            catch (InvalidCommandLineArgumentException e) {
                Console.WriteLine(e.Message);
            }
        }



        private bool ArraysContainSameStrings(string[] arr1, string[] arr2) {
            foreach (string str in arr1) {
                if (Array.IndexOf(arr2, str) < 0)
                    return false;
            }
            return true;
        }

        private string[] SplitArgs(string arg) {
            string[] args = arg.Split(',');
            ArrayList al = new ArrayList(args.Length);
            foreach (string s in args) {
                al.Add(s.Trim('\"'));
            }
            return (string[])al.ToArray(typeof(string));
        }

        private readonly string SolutionFilename = "..\\..\\..\\BuildAutoIncrement2003.sln";

        private readonly string ProjectsArg1 = "project1,project2,project3";
        private readonly string[] Projects1;

        private readonly string ProjectsArg2 = "\"project 4\",\"project 5\",\"project 6\"";
        private readonly string[] Projects2;
	}
}